package com.ziven.common.widget.index;

import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ziven.easygo.util.BiCarry;
import com.ziven.easygo.util.EasyUtils;
import com.ziven.easygo.util.LogHelper;
import com.ziven.easygo.util.Nulls;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ziven
 */
public final class IndexHelper extends IndexInterfaceProxy {

    private final List<FastScrollBlock> fastScrollBlocks;
    private final RecyclerViewScrollHelper scrollHelper;
    private final IndexCallbackImpl indexCallback;
    private BiCarry<Boolean, View> viewSelectCarry;
    private String[] indexNames;

    public static IndexHelper newInstance() {
        return new IndexHelper();
    }

    private IndexHelper() {
        fastScrollBlocks = new ArrayList<>();
        indexCallback = new IndexCallbackImpl(() -> fastScrollBlocks, (selected, view) -> Nulls.of(viewSelectCarry).doNotNull(carry -> carry.carry(selected, view)));
        scrollHelper = new RecyclerViewScrollHelper(() -> IndexHelper.this, () -> fastScrollBlocks);
    }

    /**
     * null: not touch
     * true: touch and selected
     * false: touch and not selected
     * @param carry Boolean : null, true, false
     */
    public void setViewSelectCarry(BiCarry<Boolean, View> carry) {
        viewSelectCarry = carry;
    }

    public void setRecyclerView(@NonNull RecyclerView rv) {
        scrollHelper.setRecyclerView(rv);
        indexCallback.setRecyclerView(rv);
    }

    public void setIndexGetData() {
        indexNames = new String[0];
        setIndexGetData(this);
    }

    @Override
    public void setIndexInterface(@NonNull IndexInterface ii) {
        super.setIndexInterface(ii);
        setIndexCallback(scrollHelper);
        setIndexCallback(indexCallback);
    }

    @NonNull
    @Override
    public String[] getIndexNames() {
        return indexNames != null ? indexNames : new String[0];
    }

    @Override
    public void notifyDataUpdated() {
        if(indexNames == null) {
            super.notifyDataUpdated();
            return;
        }
        List<String> indexNameList = new ArrayList<>();
        int[] index = { RecyclerView.NO_POSITION };
        String[] indexName = { null };
        EasyUtils.forEach(fastScrollBlocks, (block, position) -> {
            if(block.position == RecyclerView.NO_POSITION) {
                block.position = position;
            }
            if(!TextUtils.equals(indexName[0], block.indexName)) {
                indexName[0] = block.indexName;
                indexNameList.add(indexName[0]);
                index[0]++;
            }
            if(block.index == RecyclerView.NO_POSITION) {
                block.index = index[0];
            }
            LogHelper.log("IndexHelper", block.toString());
        });
        indexNames = indexNameList.toArray(new String[0]);
        super.notifyDataUpdated();
    }

    public void addBlock(@NonNull String indexName) {
        addBlock(FastScrollBlock.newBlock(indexName));
    }

    public void addBlock(int position, @NonNull String indexName) {
        addBlock(FastScrollBlock.newBlock(position, indexName));
    }

    public void addBlock(@NonNull String indexName, int index) {
        addBlock(FastScrollBlock.newBlock(indexName, index));
    }

    public void addBlock(int position, @NonNull String indexName, int index) {
        addBlock(FastScrollBlock.newBlock(position, indexName, index));
    }

    public void addBlock(@NonNull FastScrollBlock block) {
        fastScrollBlocks.add(block);
    }

    public void addBlocks(@NonNull List<FastScrollBlock> blocks) {
        fastScrollBlocks.addAll(blocks);
    }

    public void clearBlocks() {
        fastScrollBlocks.clear();
    }
}
